package com.winz.cultureapp.widge;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.winz.cultureapp.R;
import com.winz.cultureapp.activity.IntangibleActivity;
import com.winz.cultureapp.adapter.vp.NewsVpAdapter;
import com.winz.cultureapp.entity.LegacyTypeBean;
import com.winz.cultureapp.entity.UserSendRes;
import com.winz.cultureapp.entity.request.UserSendReq;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.CountDownManager;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.MyToast;

public class LoginPopWindow extends PopupWindow{
    private OnClickOkListener listener;

    private View window;
    private TextView mTv_title;
    private TextView mTv_register;
    private EditText mEt_code;
    private EditText mEt_phone;
    private TextView mTv_code;
    private Button mBtn_login;

    private boolean isLogin = true; // true为登录状态，false为注册状态
    private CountDownManager count;
    private Context context;

    MyToast toast;


    public LoginPopWindow(Context context){
        this(context, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        this.context = context;
        toast = new MyToast(context);
    }

    public LoginPopWindow(Context context, int width, int height) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        window = inflater.inflate(R.layout.pop_login, null);


        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(width);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(height);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);

        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //设置SelectPicPopupWindow的View
        this.setContentView(window);
        initViews(window);
        initEvents();

    }

    private void initViews(View window) {
        mTv_title = window.findViewById(R.id.tv_title);
        mTv_register = window.findViewById(R.id.tv_register);
        mEt_code = window.findViewById(R.id.et_code);
        mEt_phone = window.findViewById(R.id.et_phone);
        mTv_code = window.findViewById(R.id.tv_code);
        mBtn_login = window.findViewById(R.id.btn_login);
        count = new CountDownManager();
        count.setOnFinishedListener(new CountDownManager.OnFinishedListener() {
            @Override
            public void onFinished() {
                mTv_code.setClickable(true);
                mTv_code.setText("重新发送");
            }
        });
    }

    private void initEvents() {
        window.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                isLogin = true;
                changeState();
            }
        });
        mTv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isLogin = !isLogin;
                changeState();

            }
        });

        mBtn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null){
                    if (TextUtils.isEmpty(mEt_phone.getText().toString())){
                        toast.show("请输入手机号码");
                        return;
                    }
                    if (TextUtils.isEmpty(mEt_code.getText())){
                        toast.show("请输入验证码");
                        return;
                    }
                    listener.onClickOk(mEt_phone.getText().toString(),mEt_code.getText().toString(),isLogin);
                }

            }
        });

        mTv_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTv_code.setClickable(false);

                if (!TextUtils.isEmpty(mEt_phone.getText())){
                    sendCode(isLogin?"1":"0");
                }else{
                    toast.show("请输入手机号码");
                }

            }
        });
    }

    /**
     * 调用验证码
     */
    private void sendCode(String type) {
        HttpUitls.onPost("user_send", new UserSendReq(mEt_phone.getText().toString(),type) ,new OnResponeListener<UserSendRes>() {
            @Override
            public void onSuccess(UserSendRes bean) {
                Logger.d("user_send==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {

                    toast.show("验证码发送成功");
                    count.startCount(mTv_code,60);
                }else{
                    toast.show(bean.msg);
                }
            }

            @Override
            public void onFailed(String error) {
                Logger.d("user_send==>>", error);
            }
        });
    }

    /**
     * 改变状态
     */
    private void changeState() {
        if (isLogin){
            mEt_code.setText("");
            mEt_phone.setText("");
            mTv_title.setText("登录茂名文化云");
            mTv_register.setText("账号注册");
            mBtn_login.setText("登录");
        }else {
            mEt_code.setText("");
            mEt_phone.setText("");
            mTv_title.setText("注册茂名文化云");
            mTv_register.setText("立即登录");
            mBtn_login.setText("注册");
        }
    }




    public void dismiss(){
        super.dismiss();
    }

    public void showAsDropDown(View root){
//        if(Build.VERSION.SDK_INT >= 24){
//            Rect visibleFrame = new Rect();
//            root.getGlobalVisibleRect(visibleFrame);
//            int height = root.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
//            setHeight(height);
//        }
        super.showAsDropDown(root,0,0);

    }

    public void setOnClickOkListener(OnClickOkListener listener){
        this.listener = listener;
    }

    public interface OnClickOkListener{
        void onClickOk(String phone,String code,boolean isLogin);
    }
}
